<?php
/*
 * @Author: 熊猫
 * @Email: 461718846@qq.com
 * @Date: 2021/6/22 22:31
 * @Descript: 
 * */

namespace app\admin\controller;


use think\facade\Db;
use think\facade\Request;

class Question extends Base
{
    #问卷列表 START
    public function lst(){
        if (Request::isAjax()){
            $param = input();
            $where = [];
            $where[] = ['admin_id','=',session('admin.id')];
            if (!empty($param['title'])){
                $where[] = ['title','like','%'.$param['title'].'%'];
            }
            $data = \app\common\model\Question::with(['admin'])->where($where)->paginate($param['limit'])->toArray();
            return $this->resultTable($data['data'],$data['total']);
        }
        return view();
    }
    public function lstForm(){
        $id = input('id');
        $data = \app\common\model\Question::find($id);
        return view('lst_form',[
            'data'=>$data
        ]);
    }
    public function lstSave(){
        $param = input('post.');
        try{
            if (empty($param['id'])){
                unset($param['id']);
                $param['admin_id'] = session('admin.id');
                $carousel = new \app\common\model\Question();
                $carousel->save($param);
            }else{
                $carousel = \app\common\model\Question::find($param['id']);
                $carousel->save($param);
            }
            return $this->result();
        }catch (\Exception $e){
            return $this->result(1,$e->getMessage());
        }
    }
    public function lstDel(){
        $id = input('id');
        return \app\common\model\Question::del($id);
    }
    #问卷列表 END

    # 自定义模板 START
    public function temp(){
        $question_id = input('question_id');

        $data = \app\common\model\Question::find($question_id);
        return view('',[
            'question_id'=>$question_id,
            'data'=>$data,
        ]);
    }

    public function saveTemp(){
        $param = input();
        return \app\common\model\Question::editPost($param,['id'=>$param['id']]);
    }

//    模板页面
    public function temp2(){
        $question_id = input('question_id');

        $data = \app\common\model\Question::find($question_id);
        $data['content'] = json_decode($data['content'],true);

        return view('',[
            'question_id'=>$question_id,
            'data'=>$data,
        ]);
    }
//    获取模板
    public function getTemp(){
        $question_id = input('question_id');
        $data = \app\common\model\Question::find($question_id);
        return $data['content'];
    }
//    模板表单
    public function form(){
        $question_id = input('question_id');
        $index = input('index');
        $type = input('type');
        $insert = input('insert');
        $content = \app\common\model\Question::where('id','=',$question_id)->value('content');
        $content = json_decode($content,true);
        if (isset($content[$index]) && is_numeric($index) && $insert != 'insert'){
            $content = $content[$index];
        }else{
            $content = [];
        }
        return view($type,[
            'question_id'=>$question_id,
            'index'=>$index,
            'content'=>$content,
            'insert'=>$insert,
        ]);
    }
//    保存模板信息
    public function saveTemp2(){
        $param = input();
        $content = \app\common\model\Question::where('id','=',$param['question_id'])->value('content');
        $content = json_decode($content,true);
        if ((!isset($param['index']) || empty($param['index'])) && !is_numeric($param['index'])){
            $content[] = $param['content'];
        }elseif ($param['insert'] == 'insert'){
//            在指定index后插入
            if (count($content)-1 == $param['index']){
                $content[] = $param['content'];
            }else{
                $newContent = [];
                foreach ($content as $k=>$v){
                    if ($k == ($param['index'] + 1) ){
                        $newContent[] = $param['content'];
                    }
                    $newContent[] = $v;
                }
                $content = $newContent;
            }
        }else{
            $content[$param['index']] = $param['content'];
        }
        return \app\common\model\Question::editPost(['content'=>json_encode($content)],['id'=>$param['question_id']]);
    }
//    删除模板
    public function tempDel(){
        $param = input();
        $content = \app\common\model\Question::where('id','=',$param['question_id'])->value('content');
        $content = json_decode($content,true);
        unset($content[$param['index']]);
        $content = array_values($content);
        return \app\common\model\Question::editPost(['content'=>json_encode($content)],['id'=>$param['question_id']]);
    }
//    移动模板
    public function tempMove(){
        $index = input('index');
        $type = input('type');
        $question_id = input('question_id');

        $content = json_decode(\app\common\model\Question::where('id','=',$question_id)->value('content'),true);
        switch ($type){
//            上移
            case 'up':
                if ($index == 0){
                    return $this->result(1,'已经是在最上面了');
                }
                $indexContent = $content[$index];
                $upContent = $content[$index-1];
                $content[$index-1] = $indexContent;
                $content[$index] = $upContent;
                break;
//                下移
            case 'down':
                if ($index >= count($content)-1){
                    return $this->result(1,'已经在最底了');
                }
                $indexContent = $content[$index];
                $downContent = $content[$index+1];
                $content[$index+1] = $indexContent;
                $content[$index] = $downContent;
                break;
//                最前
            case 'upest':
                if ($index == 0){
                    return $this->result(1,'已经是在最上面了');
                }
                $indexContent = $content[$index];
                unset($content[$index]);
                array_unshift($content,$indexContent);
                break;
//                最后
            case 'downest':
                if ($index >= count($content)-1){
                    return $this->result(1,'已经在最底了');
                }
                $indexContent = $content[$index];
                unset($content[$index]);
                $content = array_values($content);
                $content[] = $indexContent;
                break;
            case 'copy':
                $indexContent = $content[$index];
                $content[] = $indexContent;
                break;
        }
        return \app\common\model\Question::editPost(['content'=>json_encode($content)],['id'=>$question_id]);
    }
//    获取小程序预览
    public function getQuestionErcode(){
        $id = input('id');
        $app = Factory::miniProgram(config('mini'));
        $page = 'pages/renwu/question_preview/question?id=' . $id;
        $response = $app->app_code->getQrCode($page);
        if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
            $filename = $response->save(app()->getRootPath() . '/public/mini/ercode/questionPreview/', $id . '.png');
            return $this->result(0,'',['src'=>'/mini/ercode/questionPreview/' . $filename]);
        }else{
            return $this->result(1,'');
        }
    }
    # 自定义模板 END
}